package com.lemon.support.web;

import com.lemon.support.base.TopEnum;

/**
 * @program: lemonnote
 * @description: 接口数据返回
 * @author: Lemontree
 * @create: 2018-05-18 23:05
 **/
public class AjaxResult<T> {
    private static final int SUCCESS_CODE = 0;
    private static final String SUCCESS_MESSAGE = "成功";
    private static final int FAIL_CODE = -1;
    private static final String FAIL_MESSAGE = "失败";

    private int code;
    private String message;
    private T data;

    private AjaxResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private AjaxResult(int code, String message) {
        this(code, message, null);
    }

    public static AjaxResult ok() {
        return new AjaxResult(SUCCESS_CODE, SUCCESS_MESSAGE);
    }

    public static AjaxResult ok(String message) {
        return new AjaxResult(SUCCESS_CODE, message);
    }

    public static <T> AjaxResult ok(T data) {
        return new AjaxResult(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    public static AjaxResult fail() {
        return new AjaxResult(FAIL_CODE, FAIL_MESSAGE);
    }

    public static AjaxResult fail(String failMsg) {
        return new AjaxResult(FAIL_CODE, failMsg);
    }

    public static <T> AjaxResult fail(T data) {
        return new AjaxResult(FAIL_CODE, FAIL_MESSAGE, data);
    }

    public static <T> AjaxResult of(int code, String message, T data) {
        return new AjaxResult(code, message);
    }

    public static <T> AjaxResult of(TopEnum topEnum, T data) {
        return of(topEnum.getCode(), topEnum.getMessage(), data);
    }

    public static AjaxResult of(TopEnum topEnum) {
        return of(topEnum.getCode(), topEnum.getMessage(), null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
